package andy.demo.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

/**
 * xml数据采用DOM方式解析
 * 
 * @author Administrator
 * 
 */
public class xmlUtil {

    /**
     * 单例模式
     */
    private static DocumentBuilderFactory documentBuilderFactory = null;

    /**
     * DocumentBuilderFactory 实例
     * 
     * @return
     */
    private static DocumentBuilderFactory getDocumentBuilderFactory() {
        if (documentBuilderFactory == null) {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
        }
        return documentBuilderFactory;
    }

    /**
     * DocumentBuilder 实例
     * 
     * @return
     */
    private static DocumentBuilder getDocumentBuilder() {
        try {
            return getDocumentBuilderFactory().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            return null;
        }
    }

    /**
     * 将XML字符串转化为Document
     * 
     * @param xmlString
     * @return <code>Document</code> interface
     */
    public static Document stringToDocument(String xmlString) {
        if (xmlString == null)
            return null;

        DocumentBuilder documentBuilder = getDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xmlString));
        try {
            return documentBuilder.parse(inputSource);
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    
    public static XmlPullParser getPullParser() throws XmlPullParserException {
        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
  
        return xpp;
    }

    public static XmlPullParser getPullParser(InputStream intputStream) throws XmlPullParserException, IOException {
        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        
        xpp.setInput(new InputStreamReader(intputStream));
        return xpp;
    }
    
    public static XmlPullParser getPullParser(String responseXml) throws XmlPullParserException, IOException {
        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        
        xpp.setInput(new StringReader(responseXml));
        return xpp;   
    }
    
//    /** Pull方式，解析 XML */
//    public String pullXMLResolve(final InputStream inputStream) {
//        
//        StringWriter xmlWriter = new StringWriter();
//        
//        try {
//            XmlPullParser xpp = xmlUtil.getPullParser(inputStream);
//
//            List<Person> personsList = null; // 保存xml的person节点
//            Person person = null;
//            StringBuffer xmlHeader = null; // 保存xml头部
//            String ele = null; // Element flag
//
//            int eventType = xpp.getEventType();
//            while (XmlPullParser.END_DOCUMENT != eventType) {
//                switch (eventType) {
//                case XmlPullParser.START_DOCUMENT:
//                    personsList = new ArrayList<Person>(); // 初始化persons
//                    xmlHeader = new StringBuffer(); // 初始化xmlHeader
//                    break;
//
//                case XmlPullParser.START_TAG:
//                    if ("root".equals(xpp.getName())) {
//                        String attrAuthor = xpp.getAttributeValue(0);
//                        String attrDate = xpp.getAttributeValue(1);
//                        xmlHeader.append("root").append("\t\t");
//                        xmlHeader.append(attrAuthor).append("\t");
//                        xmlHeader.append(attrDate).append("\n");
//                    } else if ("person".equals(xpp.getName())) {
//                        person = new Person(); // 创建person实例
//                    } else if ("id".equals(xpp.getName())) {
//                        ele = "id";
//                    } else if ("name".equals(xpp.getName())) {
//                        ele = "name";
//                    } else if ("blog".equals(xpp.getName())) {
//                        ele = "blog";
//                    } else {
//                        ele = null;
//                    }
//                    break;
//
//                case XmlPullParser.TEXT:
//                    if (null != ele) {
//                        if ("id".equals(ele)) {
//                            person.setId(Integer.parseInt(xpp.getText()));
//                        } else if ("name".equals(ele)) {
//                            person.setName(xpp.getText());
//                        } else if ("blog".equals(ele)) {
//                            person.setBlog(xpp.getText());
//                        }
//                    }
//                    break;
//
//                case XmlPullParser.END_TAG:
//                    if ("person".equals(xpp.getName())) {
//                        personsList.add(person);
//                        person = null;
//                    }
//                    ele = null;
//                    break;
//                }
//
//                eventType = xpp.next(); // 下一个事件类型
//            }
//
//            xmlWriter.append(xmlHeader);
//            int personsLen = personsList.size();
//            for (int i = 0; i < personsLen; i++) {
//                xmlWriter.append(personsList.get(i).toString());
//            }
//
//        } catch (XmlPullParserException e) { // XmlPullParserFactory.newInstance
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return xmlWriter.toString();
//    }

}
